2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_spi_ex1_3WireSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_A SPI - 3 Wire Incrementing Data Slave
34  *
35  * Description: In this example, the device is configured as a SPI slave. The
36  * initial data is written to the TX Buffer. The master will start a transfer
37  * which clears the TX buffer on the slave and causes an RX interrupt that
38  * contains the data it recieved from the master. The slave and master data
39  * values should stay in sync and if there is a problem in communication the
40  * LED on the master will turn on. The device stays in LPM0 and sends the data
41  * in the ISR. To properly demo this example connect to master version of this
42  * example and start the slave device first.
43  *
44  * MSP430i2041
45  * ------------------
46  * /|\| |
47  * | | |
48  * --|RST P1.3/UCA0SIMO|<---Data In
49  * | |
50  * | P1.2/UCA0MISO|--->Data Out
51  * | |
52  * | P1.1/UCA0CLK|<--Serial Clock
53  * | |
54  *
55  * Author: Zack Lalanne
56  ******************************************************************************/
57 #include "driverlib.h"
58 
59 volatile uint8_t TXData = 0;
60 volatile uint8_t RXData = 0;
61 
62 int main(void) {
63 
64  EUSCI_A_SPI_initSlaveParam spiSlaveConfig = {
65  EUSCI_A_SPI_MSB_FIRST, // MSB First
66  EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
67  EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // Normal Polarity
68  EUSCI_A_SPI_3PIN // 3wire mode
69  };
70 
71 
72  WDT_hold(WDT_BASE);
73 
74  // Setting P1.1, P1.2 and P1.3 as SPI pins.
75  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
76  GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3,
77  GPIO_PRIMARY_MODULE_FUNCTION);
78 
79  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
80  // ACLK is at 32kHz
81  CS_setupDCO(CS_INTERNAL_RESISTOR);
82 
83  // Configure and enable the SPI peripheral
84  EUSCI_A_SPI_initSlave(EUSCI_A0_BASE, &spiSlaveConfig);
85  EUSCI_A_SPI_enable(EUSCI_A0_BASE);
86 
87  // Put the first byte in the transfer buffer
88  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TXData);
89 
90  EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
91 
92  // Go into LPM0 with interrupts enabled
93  __bis_SR_register(LPM0_bits | GIE);
94 }
95 
96 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
97 #pragma vector=USCI_A0_VECTOR
98 __interrupt
99 #elif defined(__GNUC__)
100 __attribute__((interrupt(USCI_A0_VECTOR)))
101 #endif
102 void USCI_A0_ISR(void) {
103 
104  switch(__even_in_range(UCA0IV, USCI_SPI_UCTXIFG)) {
105  case USCI_NONE: break;
106  case USCI_SPI_UCRXIFG:
107  // Put next byte in buffer
108  EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, ++TXData);
109  break;
110  case USCI_SPI_UCTXIFG:
111  break;
112  default: break;
113  }
114 }
volatile uint8_t RXData
void USCI_A0_ISR(void)
int main(void)
volatile uint8_t TXData